<template>
  <a-modal
    title="管理网站"
    :width="850"
    :visible="visible"
    :destroyOnClose="true"
    @ok="handleOk"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-alert
        v-if="alertVisable"
        showIcon
        banner
        message="保存信息失败,请信息是否填写正确"
        type="error"
        closable
        @close="alertOnClose"
      />
      <a-form :form="form">
        <a-row :gutter="1">
          <a-col :span="12">
            <!-- 网站 -->
            <a-row :gutter="1">
              <!-- 网站名称 personName-->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="网站名称">
                  <a-input
                    v-decorator="['webName', {rules: [{ required: true, message: '请填写网站名称' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="1">
              <!-- 网站用户-->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="网站用户">
                  <a-input
                    v-decorator="['webUser', {rules: [{ required: true, message: '请填写网站用户' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="1">
              <!-- 联系人 -->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系人">
                  <a-input
                    v-decorator="['webContact', {rules: [{ required: true, message: '请填写联系人' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="1">
              <!-- 联系地址-->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系地址">
                  <a-input
                    v-decorator="['webContactAddress', {rules: [{ required: false, message: '请填写联系地址' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="1">
              <!-- 联系邮箱 webContactEmail-->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="联系邮箱">
                  <a-input
                    v-decorator="['webContactEmail', {rules: [{ required: true, message: '请填写联系邮箱' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
            <a-row :gutter="1">
              <!-- 网站号 webContactEmail-->
              <a-col :span="24">
                <a-form-item :labelCol="labelCol" :wrapperCol="wrapperCol" label="网站号">
                  <a-input
                    v-decorator="['webIcp', {rules: [{ required: true, message: '请填写网站号' }]}]"
                  />
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
          <a-col :span="12">
            <!-- 网站logo -->
            <a-row :gutter="1">
              <a-col style="text-align:left" :span="24">
                <a-form-item
                  style="margin-top: 3px; margin-bottom: 3px; margin-left: 4px; width:100%"
                  label="网站logo"
                >
                  <ImgUpload
                    v-decorator="['webLogoUrl', { initialValue: '' }, {rules: [{ required: false }]}]"
                  >
                  </ImgUpload>
                </a-form-item>
              </a-col>
            </a-row>
            <!-- 网站二维码 -->
            <a-row :gutter="1">
              <a-col style="text-align:left" :span="24">
                <a-form-item
                  style="margin-top: 3px; margin-bottom: 3px; margin-left: 4px; width:100%"
                  label="网站二维码"
                >
                  <ImgUpload
                    v-decorator="['webQrcodeUrl', { initialValue: '' }, {rules: [{ required: false }]}]"
                  >
                  </ImgUpload>
                </a-form-item>
              </a-col>
            </a-row>
          </a-col>
        </a-row>
      </a-form>
    </a-spin>
  </a-modal>
</template>

<script>
import { saveInfo, getInfoByPid } from './webinfo'
import ImgUpload from '@/views/sys/modules/upload/ImgUpload'

export default {
  name: 'WebinfoModal',
  components: {
    // TimePicker
    ImgUpload
  },
  props: {
    // theOrgPid: { type: String, default: '' },
    // visible: { type: Boolean, default: false }
  },
  data () {
    return {
      // form: this.$form.createForm(this),
      dateFormat: 'YYYY-MM-DD',
      labelCol: {
        xs: { span: 24 },
        sm: { span: 5 }
      },
      wrapperCol: {
        xs: { span: 24 },
        sm: { span: 16 }
      },
      confirmLoading: true, // 查询加载状态
      alertVisable: false, // 错误提示框
      objInfo: {
        sysSeq: 0,
        pid: ''
      },
      visible: false
    }
  },
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {
  },
  methods: {
    // 关闭提示
    alertOnClose () {
      this.alertVisable = false
    },
    showAddModal () {
      this.objInfo = {
        sysSeq: 0,
        pid: ''
      }
      this.confirmLoading = false
      this.visible = true
    },
    // 查对象信息
    async findObjInfo (pid, headers) {
      this.confirmLoading = true
      const res = await getInfoByPid(pid, headers)
      this.confirmLoading = false
      if (res && res.code && res.code === 'C1' && res.data) {
        this.objInfo.sysSeq = res.data.sysSeq
        this.objInfo.pid = res.data.pid
        this.form.setFieldsValue(res.data)
      }
    },
    // 保存方法
    async saveThisObj (obj, headers) {
      const _this = this
      this.confirmLoading = true
      const res = await saveInfo(obj, headers)
      _this.confirmLoading = false
      if (res && res.code && res.code === 'C1' && res.data) {
        _this.$message.success('保存成功')
        _this.$emit('ok')
        _this.close()
      } else {
        _this.alertVisable = true
      }
    },
    // 编辑信息
    showEditModal (record) {
      const pid = record.pid
      const headers = {
        authOption: '1'
      }
      if (pid) {
        this.findObjInfo(pid, headers)
      }
      this.visible = true
    },
    close () {
      this.alertVisable = false
      this.$emit('close')
      this.visible = false
    },
    // 保存按钮
    async handleOk () {
      const _this = this
      // 触发表单验证
      _this.form.validateFields((err) => {
        // 验证表单没错误
        if (!err) {
          _this.saveThisObj(Object.assign(_this.objInfo, _this.form.getFieldsValue()), {})
        }
      })
    },
    handleCancel () {
      this.close()
    }

  }
}
</script>

<style>
  .avatar-uploader > .ant-upload {
    width: 200px;
    height: 200px;
  }
  .ant-upload-select-picture-card i {
    font-size: 32px;
    color: #999;
  }

  .ant-upload-select-picture-card .ant-upload-text {
    margin-top: 8px;
    color: #666;
  }
</style>
